#ifndef THINK_TRAITS_MODEL_SOFTDELETE_H
#define THINK_TRAITS_MODEL_SOFTDELETE_H

static char* traits_model_softdelete =
	"\n"
	"\n"
	"namespace traits\\model;\n"
	"\n"
	"use think\\Collection;\n"
	"use think\\db\\Query;\n"
	"use think\\Model;\n"
	"\n"
	"/**\n"
	" * @mixin \\Think\\Model\n"
	" */\n"
	"trait SoftDelete\n"
	"{\n"
	"    /**\n"
	"     * 判断当前实例是否被软删除\n"
	"     * @access public\n"
	"     * @return boolean\n"
	"     */\n"
	"    public function trashed()\n"
	"    {\n"
	"        $field = $this->getDeleteTimeField();\n"
	"\n"
	"        if ($field && !empty($this->data[$field])) {\n"
	"            return true;\n"
	"        }\n"
	"        return false;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 查询包含软删除的数据\n"
	"     * @access public\n"
	"     * @return Query\n"
	"     */\n"
	"    public static function withTrashed()\n"
	"    {\n"
	"        return (new static )->getQuery();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 只查询软删除数据\n"
	"     * @access public\n"
	"     * @return Query\n"
	"     */\n"
	"    public static function onlyTrashed()\n"
	"    {\n"
	"        $model = new static();\n"
	"        $field = $model->getDeleteTimeField(true);\n"
	"\n"
	"        if ($field) {\n"
	"            return $model->getQuery()->useSoftDelete($field, [\'not null\', \'\']);\n"
	"        } else {\n"
	"            return $model->getQuery();\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 删除当前的记录\n"
	"     * @access public\n"
	"     * @param bool $force 是否强制删除\n"
	"     * @return integer\n"
	"     */\n"
	"    public function delete($force = false)\n"
	"    {\n"
	"        if (false === $this->trigger(\'before_delete\', $this)) {\n"
	"            return false;\n"
	"        }\n"
	"\n"
	"        $name = $this->getDeleteTimeField();\n"
	"        if ($name && !$force) {\n"
	"            // 软删除\n"
	"            $this->data[$name] = $this->autoWriteTimestamp($name);\n"
	"            $result            = $this->isUpdate()->save();\n"
	"        } else {\n"
	"            // 强制删除当前模型数据\n"
	"            $result = $this->getQuery()->where($this->getWhere())->delete();\n"
	"        }\n"
	"\n"
	"        // 关联删除\n"
	"        if (!empty($this->relationWrite)) {\n"
	"            foreach ($this->relationWrite as $key => $name) {\n"
	"                $name   = is_numeric($key) ? $name : $key;\n"
	"                $result = $this->getRelation($name);\n"
	"                if ($result instanceof Model) {\n"
	"                    $result->delete();\n"
	"                } elseif ($result instanceof Collection || is_array($result)) {\n"
	"                    foreach ($result as $model) {\n"
	"                        $model->delete();\n"
	"                    }\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"\n"
	"        $this->trigger(\'after_delete\', $this);\n"
	"\n"
	"        // 清空原始数据\n"
	"        $this->origin = [];\n"
	"\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 删除记录\n"
	"     * @access public\n"
	"     * @param mixed $data  主键列表(支持闭包查询条件)\n"
	"     * @param bool  $force 是否强制删除\n"
	"     * @return integer 成功删除的记录数\n"
	"     */\n"
	"    public static function destroy($data, $force = false)\n"
	"    {\n"
	"        if (is_null($data)) {\n"
	"            return 0;\n"
	"        }\n"
	"\n"
	"        // 包含软删除数据\n"
	"        $query = (new static())->db(false);\n"
	"        if (is_array($data) && key($data) !== 0) {\n"
	"            $query->where($data);\n"
	"            $data = null;\n"
	"        } elseif ($data instanceof \\Closure) {\n"
	"            call_user_func_array($data, [ & $query]);\n"
	"            $data = null;\n"
	"        }\n"
	"\n"
	"        $count = 0;\n"
	"        if ($resultSet = $query->select($data)) {\n"
	"            foreach ($resultSet as $data) {\n"
	"                $result = $data->delete($force);\n"
	"                $count += $result;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        return $count;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 恢复被软删除的记录\n"
	"     * @access public\n"
	"     * @param array $where 更新条件\n"
	"     * @return integer\n"
	"     */\n"
	"    public function restore($where = [])\n"
	"    {\n"
	"        if (empty($where)) {\n"
	"            $pk         = $this->getPk();\n"
	"            $where[$pk] = $this->getData($pk);\n"
	"        }\n"
	"\n"
	"        $name = $this->getDeleteTimeField();\n"
	"\n"
	"        if ($name) {\n"
	"            // 恢复删除\n"
	"            return $this->getQuery()\n"
	"                ->useSoftDelete($name, [\'not null\', \'\'])\n"
	"                ->where($where)\n"
	"                ->update([$name => null]);\n"
	"        } else {\n"
	"            return 0;\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 查询默认不包含软删除数据\n"
	"     * @access protected\n"
	"     * @param Query $query 查询对象\n"
	"     * @return Query\n"
	"     */\n"
	"    protected function base($query)\n"
	"    {\n"
	"        $field = $this->getDeleteTimeField(true);\n"
	"        return $field ? $query->useSoftDelete($field) : $query;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取软删除字段\n"
	"     * @access public\n"
	"     * @param bool $read 是否查询操作(写操作的时候会自动去掉表别名)\n"
	"     * @return string\n"
	"     */\n"
	"    protected function getDeleteTimeField($read = false)\n"
	"    {\n"
	"        $field = property_exists($this, \'deleteTime\') && isset($this->deleteTime) ?\n"
	"        $this->deleteTime :\n"
	"        \'delete_time\';\n"
	"\n"
	"        if (false === $field) {\n"
	"            return false;\n"
	"        }\n"
	"\n"
	"        if (!strpos($field, \'.\')) {\n"
	"            $field = \'__TABLE__.\' . $field;\n"
	"        }\n"
	"\n"
	"        if (!$read && strpos($field, \'.\')) {\n"
	"            $array = explode(\'.\', $field);\n"
	"            $field = array_pop($array);\n"
	"        }\n"
	"\n"
	"        return $field;\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif